package com.woniu.service.imp;/*
 *
 * @author   米雷
 * @Description
 * @Date:create in 19:08 2020-03-28
 *
 * */

import com.woniu.dao.BackServerMapper;
import com.woniu.dao.OrdersMapper;
import com.woniu.dao.ServerSubscribeMapper;
import com.woniu.dto.OrderAndDetail;
import com.woniu.dto.SubscribeToServer;
import com.woniu.pojo.ServerSubscribe;
import com.woniu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImp implements OrderService {

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    ServerSubscribeMapper serverSubscribeMapper;
    @Autowired
    BackServerMapper backServerMapper;

    @Override
    public OrderAndDetail getUserOrder(Long id)
    {
        return ordersMapper.getOrdersDetail(id);
    }

    @Override
    public String deleteOrder(Long id)
    {
        int i = ordersMapper.deleteByUserId(id);
        if (i != 0)
        {
            return "取消成功";
        } else
        {
            return "取消失败";
        }
    }

    @Override
    public String cancelOrder(Long uid, Long sid, String mes)
    {
        int i = serverSubscribeMapper.updateByDBId(sid);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String date = dateFormat.format(new Date());
        int x = backServerMapper.insertByid(uid, sid, mes, date);
        if (i!=0&&x!=0){
            return "取消成功";
        }else {
            return "取消失败";
        }

    }

    @Override
    public List<SubscribeToServer> getMySub(Long id)
    {
        return serverSubscribeMapper.getMySub(id);
    }
}
